<template>
  <div class="page gray-page inspect-page">
    <nav-bar :title="title" border></nav-bar>
    <div class="page-main" style="margin-top: 44px">
      <inspect-head v-model="gridCode" type="company" @change="getInspect"></inspect-head>
      <div class="table-list">
        <div class="table-list-head">
          <van-row>
            <van-col :span="11">重点单位名称</van-col>
            <van-col :span="9">上次巡查时间</van-col>
            <van-col :span="4" align="center">操作</van-col>
          </van-row>
        </div>
        <template v-if="inspectPoint.length">
          <div
            class="table-list-row"
            :class="key % 2 ? 'table-list-row__even' : 'table-list-row__odd'"
            v-for="(item, key) in inspectPoint"
            :key="key"
          >
            <van-row type="flex">
              <van-col :span="11">
                <p class="van-ellipsis">{{ item.companyName }}</p>
              </van-col>
              <van-col :span="9">
                <!-- 已巡查 -->
                <template v-if="item.inspectionPointRecord.length">
                  <!-- table-list-row__status_abnormal：有隐患 || 隐患未解决 -->
                  <!-- table-list-row__status_normal：无隐患 || 隐患已解决 -->
                  <span
                    class="table-list-row__status"
                    :class="{
                      'table-list-row__status_abnormal':
                        (item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult ===
                            '0') ||
                        (!item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].inspectionResult === '1'),
                      'table-list-row__status_normal':
                        (item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult ===
                            '1') ||
                        (!item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].inspectionResult === '0')
                    }"
                  >
                    {{ item.inspectionPointRecord[item.inspectionPointRecord.length - 1].inspectionTime.split(' ')[0] }}
                    <!-- {{ item.inspectionPointRecord[item.inspectionPointRecord.length - 1].inspectionTime.split(' ')[1] }} -->
                    <!-- 有隐患 || 隐患未解决 -->
                    <van-icon
                      name="warning"
                      v-if="
                        (item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult ===
                            '0') ||
                        (!item.inspectionPointRecord[item.inspectionPointRecord.length - 1].latentDangerResult &&
                          item.inspectionPointRecord[item.inspectionPointRecord.length - 1].inspectionResult === '1')
                      "
                    />
                    <br />
                    {{ item.isCurrentMonth === '0' ? '本月未巡查' : '' }}
                  </span>
                </template>
                <!-- 未巡查 -->
                <template v-else>
                  <span class="table-list-row__status">本月未巡查</span>
                </template>
              </van-col>
              <van-col :span="4">
                <template v-if="item.inspectionPointRecord.length">
                  <van-button
                    type="info"
                    plain
                    size="mini"
                    @click="
                      $router.push({
                        path: `/inspectvisit/inspect/company/detail`,
                        query: {
                          type: 'detail',
                          gridCode,
                          inspectionPointCode: item.companyCode,
                          date: $route.query.date
                        }
                      })
                    "
                  >
                    查看
                  </van-button>
                </template>
                <template v-else>
                  <van-button type="info" size="mini" @click="handleInspect(item)">巡查</van-button>
                </template>
              </van-col>
            </van-row>
          </div>
        </template>
        <template v-else>
          <div class="table-list-row table-list-row__old">
            <van-row type="flex">
              <van-col :span="24"><div class="table-list-row__nodata">暂无数据</div></van-col>
            </van-row>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>
<script>
import InspectVisit from '@/api/inspectVisit'
import InspectHead from '@/components/Widget/InspectVisit/Inspect/head'
import { formatTimes } from '@/libs/tool'
import mapPlugin from '@/libs/mapPlugin'
// 定义一些常量
var PI = 3.1415926535897932384626
var a = 6378245.0
var ee = 0.00669342162296594323
export default {
  components: { InspectHead },
  data() {
    return {
      title: '重点单位巡查',
      gridCode: '',
      inspectPoint: []
    }
  },
  created() {
    this.gridCode = this.$route.query.gridCode
  },
  mounted() {},
  methods: {
    /**
     * WGS84转GCj02
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    wgs84togcj02(lng, lat) {
      if (this.out_of_china(lng, lat)) {
        return [lng, lat]
      } else {
        var dlat = this.transformlat(lng - 105.0, lat - 35.0)
        var dlng = this.transformlng(lng - 105.0, lat - 35.0)
        var radlat = (lat / 180.0) * PI
        var magic = Math.sin(radlat)
        magic = 1 - ee * magic * magic
        var sqrtmagic = Math.sqrt(magic)
        dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI)
        dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI)
        var mglat = lat + dlat
        var mglng = lng + dlng
        return [mglng, mglat]
      }
    },

    getInspect() {
      InspectVisit.Company.list({
        gridCode: this.gridCode,
        date: this.$route.query.date || formatTimes.formatDate(new Date())
      }).then(res => {
        const { status, data } = res.data
        if (status === 0) {
          this.inspectPoint = data.gridInspectionPoint
        }
      })
    },
    // 巡查
    handleInspect(item) {
      mapPlugin.geolocation(async result => {
        const lnglat1 = result.position
        const data = await this.wgs84togcj02(item.longitude, item.latitude)
        const lnglat2 = [data[0].toFixed(8), data[1].toFixed(8)]
        const distance = lnglat1.distance(lnglat2)
        if (distance <= 100) {
          this.$router.push({
            path: '/inspectvisit/inspect/company/report',
            query: {
              gridCode: this.gridCode,
              inspectionPointCode: item.companyCode
            }
          })
        } else {
          this.$toast({
            message: '距离当前巡查点过远，请重试',
            position: 'bottom'
          })
        }
      })
    },
    transformlat(lng, lat) {
      var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng))
      ret += ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0) / 3.0
      ret += ((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) / 3.0
      ret += ((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) * 2.0) / 3.0
      return ret
    },
    transformlng(lng, lat) {
      var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng))
      ret += ((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0) / 3.0
      ret += ((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) / 3.0
      ret += ((150.0 * Math.sin((lng / 12.0) * PI) + 300.0 * Math.sin((lng / 30.0) * PI)) * 2.0) / 3.0
      return ret
    },
    /**
     * 判断是否在国内，不在国内则不做偏移
     * @param lng
     * @param lat
     * @returns {boolean}
     */
    out_of_china(lng, lat) {
      return lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271 || false
    }
  }
}
</script>
<style scoped lang="less">
@import '~@/assets/style/inspectVisit/inspect/index.less';
@import '~@/assets/style/table.less';
</style>
